package com.ncp.servlet.article;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUpload;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.RequestContext;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.servlet.ServletRequestContext;

import com.ncp.model.biz.article.ArticleManager;

public class ArticleAdd extends HttpServlet {

	public ArticleAdd() {
	super();
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
	   throws ServletException, IOException {
		boolean b=false;
		String s[]=new String[4];
		request.setCharacterEncoding("UTF-8");
		RequestContext requestContext = new ServletRequestContext(request);
		String path1 = request.getServerName();
		String path2 = request.getRealPath(path1);
		String url="images/news";
		int n=path2.lastIndexOf("\\");
		String S_realPath=path2.substring(0,n);
		String realPath =S_realPath+"\\"; 
		if(FileUpload.isMultipartContent(requestContext)){
		   DiskFileItemFactory factory = new DiskFileItemFactory();
		   factory.setRepository(new File("D:/image/"));
		   ServletFileUpload upload = new ServletFileUpload(factory);
		   upload.setSizeMax(-1);
		   List items = new ArrayList();
		   try {
			   items = upload.parseRequest(request);
		   } catch (FileUploadException e1) {
			   System.out.println("上传出错" + e1.getMessage());
		    }
		   Iterator it = items.iterator();
		   int i=-1;
		   while(it.hasNext()){
			   i++;
			   FileItem fileItem = (FileItem) it.next();
			   if(fileItem.isFormField()){   
		    	s[i]=new String(fileItem.getString().getBytes("iso8859-1"), "UTF-8");
		    	//System.out.println(s[i]);
		    }else{
		    	
		    	//System.out.println(fileItem.getName());
		    	 String tempStr1[]=ArticleManager.split(fileItem.getName(),"\\");
		    	 s[i]=tempStr1[tempStr1.length-1];
		    	 String tempStr2[]=ArticleManager.split(s[i],".");
		    	 s[i]=UUID.randomUUID().toString()+"."+tempStr2[tempStr2.length-1];
			     if(fileItem.getName()!=null && fileItem.getSize()!=0){
			     File fullFile = new File(fileItem.getName());
			     File newFile= new File(realPath+url+"/"+ s[i]);
			     try {
			    	 fileItem.write(newFile);
			     } catch (Exception e) {
			    	 e.printStackTrace();
			      }
			     }else{
			    	 System.out.println("文件没有找到");
			     }
			 }
		
		   }
		}
			
		b=new ArticleManager().addArticle(s[0],s[3],s[1],s[2]);
		if(b){
			request.setAttribute("url", "pages/admin/argaculturWorld/articleAdd.jsp");
			request.getRequestDispatcher("pages/admin/operateOK.jsp").forward(request, response);
		}
	}
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		this.doPost(request, response);
	}

}
